package com.ny.view;

import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;
import com.ny.service.StudentService;
import com.sun.deploy.cache.BaseLocalApplicationProperties;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class IndexView {
    private static StudentService studentService = new StudentServiceImpl();
    private static ProductService productService = new ProductServiceImpl();
    private static OrderService orderService = new OrderServiceImpl();

    public static void main(String[] args) throws SQLException {
        while (true) {
            System.out.println("欢迎来到校园商品系统");
            System.out.println("请选择");
            System.out.println("1.用户登录");
            System.out.println("2.注册");
            System.out.println("3.管理员登录");
            System.out.println("4.退出");
            System.out.println("请输入选择的序号");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            switch (n) {
                case 1:
                    System.out.println("请输入用户名：");
                    String sname = scanner.next();
                    System.out.println("请输入密码：");
                    String spassword = scanner.next();
                    Student student = studentService.selectStudent(sname, spassword);
                    if (student != null) {
                        System.out.println("登录成功");
                        List<Product> productList = productService.getAllProducts();
                        System.out.println("商品id\t商品名称\t商品价格\t商品数量\t商品种类\t商品描述");
                        for (Product product : productList) {
                            System.out.println(product.getProductId()+"\t");
                            System.out.println(product.getProductName()+"\t");
                            System.out.println(product.getPrice()+"\t");
                            System.out.println(product.getStock()+"\t");
                            System.out.println(product.getCategory()+"\t");
                            System.out.println(product.getDescription()+"\t");
                            product.getPrice();
                        }
                        System.out.println("请选择购买的商品编号");
                        int productId = scanner.nextInt();
                        System.out.println("请选择购买数量");
                        int num = scanner.nextInt();

                        Order  order = new Order();
                        order.setStudentId(student.getStudentId());
                        order.setProductId(productId);
                        order.setNumber(num);
                        order.setTotalPrice(num  * productList.get(productId-1).getPrice());
                        int i = orderService.insertOrder(order);
                        if(i>0){
                            System.out.println("购买成功");
                        }
                        else {
                            System.out.println("购买失败");
                        }
                    }
                    else {
                        System.out.println("登录失败");
                    }
                    break;
                case 2:
                    System.out.println("实现注册");
                    System.out.println("请输入用户名");
                    String name = scanner.next();
                    System.out.println("请输入密码");
                    String password = scanner.next();
                    System.out.println("请输入手机号码");
                    String phone = scanner.next();

                    Student s = new Student();
                    s.setName(name);
                    s.setPassword(password);
                    s.setPhone(phone);
                    int i = studentService.insertStudent(s);
                    if (i>0) {
                        System.out.println("注册成功");
                    } else {
                        System.out.println("注册失败");
                    }
                    break;
                case 3:
                    System.out.println("实现管理员登录");
                    break;
                case 4:
                    System.out.println("退出系统");
                    return;

               }
        }
    }
}